import getopt
import sys

import tax_bonus

def printUsage():
    print("Usage:\n"
          "-h: print this message\n"
          "-l: print all tax details on each level\n"
          "-p: set price per stock in foreign currency\n"
          "-s: set stock sum\n"
          "-r: set rate of foreign currency / local currency")
    sys.exit(1)


class StockCalculator:
    def __init__(self, price, sum, rate):
        self.price = price
        self.sum = sum
        self.rate = rate

    def calculate(self):
        self.totalInForeign = price * sum
        self.totalInLocal = self.totalInForeign * self.rate
        self.taxCalculator = tax_bonus.BonusTaxCalculator(self.totalInLocal)
        self.taxCalculator.calculate()

    def printDetails(self):
        print("Price             :%f\n"
              "Sum               :%f\n"
              "Rate              :%f\n"
              "Total (foreign)   :%f"
              % (self.price, self.sum, self.rate, self.totalInForeign))
        self.taxCalculator.printDetails()


if __name__ == '__main__':
    opts, args = getopt.getopt(sys.argv[1:], "hp:s:r:l")
    for op, value in opts:
        if op == "-h":
            printUsage()
        elif op == "-p":
            price = float(value)
        elif op == "-s":
            sum = int(value)
        elif op == "-r":
            rate = float(value)
        elif op == "-l":
            tax_bonus.BonusTaxCalculator.printAllTaxLevels()
            sys.exit(0)
        else:
            print("Unsupported option ", op)
            sys.exit(1)
    if 'price' not in vars() or 'sum' not in vars() or 'rate' not in vars():
        printUsage()
    # tax_bonus.BonusTaxCalculator.printAllTaxLevels()
    calculator = StockCalculator(price, sum, rate)
    calculator.calculate()
    calculator.printDetails()